#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import platform
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


def wait_for_xpath(driver, xpath, timeout=30):
    try:
        WebDriverWait(driver, timeout, 0.5).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
        return driver.find_element_by_xpath(xpath)
    except:
        return False


def reply_topic(driver):
    reply_content = driver.find_elements('xpath', '//*[@id="noteContainer"]/div[4]/div[3]/div/div/div[1]/div[1]/div/div/span')
    if reply_content:
        reply_content[0].click()
        reply_el = driver.find_elements('xpath', '//*[@id="content-textarea"]')
        if reply_el:
            reply_el[0].send_keys('咨询 咨询  咨询')
            # 获取放松按钮
            reply_send_btn = driver.find_elements('xpath', '//*[@id="noteContainer"]/div[4]/div[3]/div/div/div[2]/div/div[2]/button[1]')
            if reply_send_btn:
                reply_send_btn[0].click()
                time.sleep(1)


def reply_comment(driver, keywords, content):
    no_comments = driver.find_elements('xpath', '//*[@id="noteContainer"]/div[4]/div[2]/div[3]/div/p')
    if no_comments:
        print(no_comments[0].text)
        print('没有评论')
    else:
        comment_list = driver.find_elements('xpath', '//*[@id="noteContainer"]/div[4]/div[2]/div[3]/div/div[2]/div')
        if comment_list:
            for comment in comment_list:
                # 获取评论内容
                comment_content_els = comment.find_elements('xpath', 'div/div/div[2]/div[2]')
                if comment_content_els:
                    comment_content = comment_content_els[0].text
                    if comment_content:
                        has_keywords = False
                        for keyword in keywords:
                            if keyword in comment_content:
                                has_keywords = True
                                break
                        if has_keywords:
                            # 获取回复按钮
                            reply_btn = comment.find_elements('xpath', 'div/div/div[2]/div[3]/div[2]/div[2]')
                            if reply_btn:
                                print(reply_btn[0].text)
                                reply_btn[0].click()
                                time.sleep(1)
                                # 设置评论内容并且点击发送
                                reply_content = driver.find_elements('xpath', '//*[@id="content-textarea"]')
                                if reply_content:
                                    # 设置发送内容
                                    reply_content[0].send_keys(content)
                                    # 获取放松按钮
                                    reply_send_btn = driver.find_elements('xpath', '//*[@id="noteContainer"]/div[4]/div[3]/div/div/div[3]/div/div[2]/button[1]')
                                    if reply_send_btn:
                                        reply_send_btn[0].click()
                                    time.sleep(1)
                            else:
                                print('---没找到回复按钮')
                        else:
                            print('---评论没有关键词')
                    else:
                        print('没有获取到评论内容')
                else:
                    print('没有获取到评论内容')

def get_webdriver(url, keywords, content, version, port):
    # service_path_temp = '/Users/limenghou/work/yhdd/spider_selenium/spider/data/xhs/{dir}/chromedriver{version}'
    service_path_temp = '{dir}/chromedriver{version}'
    if platform.system() == 'Windows':
        service_path = service_path_temp.format(dir='win', version=version)
    elif platform.system() == 'Darwin':
        service_path = service_path_temp.format(dir='mac', version=version)
    else:
        service_path = service_path_temp.format(dir='mac', version='129')
    service = Service()
    print(service_path)
    service.executable_path = service_path
    debuggerAddress = '127.0.0.1:{port}'.format(port=port)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("debuggerAddress", debuggerAddress)
    driver = webdriver.Chrome(options=chrome_options, service=service)
    driver.implicitly_wait(5)
    # driver.get('https://www.xiaohongshu.com/user/profile/66ab811a000000000b033475')
    # driver.get('https://www.xiaohongshu.com/user/profile/5b484031f7e8b962409bcb9e')
    # driver.get('https://www.xiaohongshu.com/user/profile/5d9175300000000001018fa3')
    driver.get(url)
    time.sleep(2)
    elements = driver.find_elements('xpath', '//*[@id="userPostedFeeds"]/section')
    if elements:
        for element in elements:
            title = element.find_elements('xpath', 'div/div/a/span')
            if title:
                print(title[0].text)
            element.click()
            time.sleep(2)
            # 回复帖子
            # reply_topic(driver)
            # 回复评论
            reply_comment(driver, keywords, content)
            close_el = driver.find_elements('xpath', '/html/body/div[5]/div[2]')
            if close_el:
                close_el[0].click()
            else:
                close_el = driver.find_elements('xpath', '/html/body/div[4]/div[2]')
                if close_el:
                    close_el[0].click()
            time.sleep(1)


def start_replay(url, keywords, content, version, port):
    while True:
        print('-------')
        keywords_arr = keywords.split(',')
        get_webdriver(url, keywords_arr, content, version, port)
        time.sleep(20)